package com.tree;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Author:hanguanglei
 * @Description:
 * @Date: Created in 2020-10-06 14:51
 * @Modified By:
 */

public class LCATool {
    // 节点数据文件
    private String dataFilePath;
    // 查询请求数据文件
    private String queryFilePath;
    // 请求数据组
    private ArrayList<int[]> querys;
    // 请求结果值
    private int[][] resultValues;
    // 初始数据值
    private ArrayList<String> totalDatas;

    private List<TreeNode> treeNodeList=new ArrayList<>();

    public LCATool(String dataFilePath) {
        this.dataFilePath = dataFilePath;
        ArrayList<String[]> arrayList=readDataFile();
        for (int i=0;i<arrayList.size();i++) {
            String[] arr = arrayList.get(i);
            TreeNode treeNode = new TreeNode(arr[0]);
            for (int j = 1; j < arr.length; j++) {
                TreeNode chtreeNode = new TreeNode(arr[j]);
                treeNode.addNode(chtreeNode);
            }
            treeNodeList.add(treeNode);
        }
    }


    public List<TreeNode> getTreeNodeList() {
        return treeNodeList;
    }
    /**
     * 从文件中读取数据
     */
    public ArrayList<String[]> readDataFile() {
        File file = new File(dataFilePath);
        ArrayList<String[]> dataArray = new ArrayList<String[]>();

        try {
            BufferedReader in = new BufferedReader(new FileReader(file));
            String str;
            String[] tempArray;
            while ((str = in.readLine()) != null) {
                tempArray = str.split(",");
                dataArray.add(tempArray);
            }
            in.close();



        } catch (IOException e) {
            e.getStackTrace();
        }

        return dataArray;
    }


}
